package com.lsh.service.impl;

import com.lsh.service.CommonService;
import com.lsh.util.HttpConnectionUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;

@Service
public class CommonServiceImpl implements CommonService {
    /**
     * 天气预报业务
     * @param province
     * @param city
     * @return
     */
    @Override
    public String weatherForecast(String province, String city) {
        StringBuffer sb = new StringBuffer();
        sb.append("https://wis.qq.com/weather/common?source=pc&weather_type=observe|forecast_24h&province=")
                .append(province).append("&city=").append(city);
        String htmlResult = HttpConnectionUtil.getHtmlResponse(sb.toString());
        String data = JSONObject.fromObject(htmlResult).get("data").toString();
        JSONObject joForecast = (JSONObject) JSONObject.fromObject(data).get("forecast_24h");
        ArrayList al = new ArrayList();

        Calendar calendar = Calendar.getInstance();
        String currentDate = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());

        for(int i=0;i<joForecast.size();i++){
            JSONObject j = (JSONObject) joForecast.get(String.valueOf(i));
            LinkedHashMap<String, String> weatherNumber = new LinkedHashMap<>();
            if(((j.get("time").toString()).compareTo(currentDate)) < 0 ){
                continue;
            }
            weatherNumber.put("day_time", j.get("time").toString());
            weatherNumber.put("day_weather", j.get("day_weather").toString()); // 当前天气
            weatherNumber.put("day_weather_code", j.get("day_weather_code").toString()); // 对应天气图片编号
            weatherNumber.put("day_wind_direction", j.get("day_wind_direction").toString()); // 当前风向
            weatherNumber.put("day_wind_power", j.get("day_wind_power").toString()); // 当前风力
            weatherNumber.put("max_degree", j.get("max_degree").toString()); // 最大温度"
            weatherNumber.put("min_degree", j.get("min_degree").toString()); // 最小温度
            JSONObject jsonObject = new JSONObject();
            jsonObject.putAll(weatherNumber);
            al.add(jsonObject);
        }
        JSONObject jo = new JSONObject();
        jo.clear();
        jo.put("weather", al);
        return jo.toString();
    }

    /**
     * 高铁车班业务
     * @param go
     * @param arrive
     * @return
     */
    @Override
    public String highSpeedTrain(String go, String arrive) {
        return null;
    }
}
